package com.aerozhonghuan.oknet2;

import android.text.TextUtils;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.Request;
import okhttp3.RequestBody;

/**
 * RequestConvert
 * Created by zhangyunfei on 16/3/11.
 */
public class RequestConvert {
    private static final MediaType MEDIA_TYPE_STREAM = MediaType.parse("application/octet-stream");

    public static Request convert(BodyStringRequest inRequest) {
        MediaType mediaType = inRequest.getMediaType();
        RequestBody requestBody = RequestBody.create(mediaType, inRequest.getBodyString());
        Request.Builder builder = new Request.Builder()
                .url(inRequest.getUrl())
                .post(requestBody)
                .addHeader("Accept-Encoding", "gzip,deflate");

        buildHeaderPara(inRequest.getHeaders(), builder);
        return builder.build();
    }

    private static void buildHeaderPara(HashMap<String, String> header, Request.Builder builder) {
        if (header != null && header.size() > 0) {
            for (String $i : header.keySet()) {
                builder.addHeader($i, header.get($i));
            }
        }
    }

    public static Request convert(CommonRequest inRequest) {
        boolean useGetMethod = inRequest.useGetMethod();

        Request.Builder builder = new Request.Builder()
                .url(inRequest.getUrl());
        if (!useGetMethod) {
            FormBody.Builder formBodyBuilder = new FormBody.Builder();
            if (null != inRequest.getParas()) {
                for (Map.Entry<String, String> item : inRequest.getParas().entrySet()) {
                    if (!TextUtils.isEmpty(item.getValue())) {
                        formBodyBuilder.add(item.getKey(), item.getValue() + "");
                    }
                }
            }
            builder.post(formBodyBuilder.build());
        }
        builder.addHeader("Accept-Encoding", "gzip,deflate");

        buildHeaderPara(inRequest.getHeaders(), builder);
        return builder.build();
    }

    public static Request convert(FileUploadRequest inRequest) {
        MultipartBody.Builder builder = new MultipartBody.Builder()
                .setType(MultipartBody.FORM);
        for (Map.Entry<String, String> item : inRequest.getParas().entrySet()) {
            if (!TextUtils.isEmpty(item.getValue())) {
                builder.addFormDataPart(item.getKey(), item.getValue() + "");
            }
        }
        for (Map.Entry<String, File> item : inRequest.getFileParas().entrySet()) {
            builder.addFormDataPart(item.getKey(), item.getValue().getName(),
                    RequestBody.create(MEDIA_TYPE_STREAM, item.getValue()));
        }
        MultipartBody requestBody = builder.build();
        Request.Builder requestBuidler = new Request.Builder()
                .url(inRequest.getUrl())
                .post(requestBody);
        buildHeaderPara(inRequest.getHeaders(), requestBuidler);
        return requestBuidler.build();
    }

}
